package chen.web.util.ubb.standard;

import chen.web.util.ubb.AbstractTag;
import chen.web.util.ubb.UbbTag;

/**
 * 支持格式：[url]http://xxx.com[/url]转换成<a href="http://xxx.com">http://xxx.com</a>
 * [url=http://xxx.com]xxx[/url]转换成<a href="http://xxx.com">xxx</a>
 * @author chen
 *
 */
public class UrlTag extends AbstractTag {

	public static final String TagName = "url";
	//是否在新窗口中打开
	private boolean blank = true;

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return TagName;
	}

	@Override
	public void toHtml(StringBuilder sb) {
		// TODO Auto-generated method stub
		String url = super.getProperty();
		//不带描文形式
		if((url == null || url.length() == 0) && isClosed()){
			StringBuilder inner = new StringBuilder();
			super.innerUbb(inner, false);
			url = inner.toString();
		}
		if(url == null || url.length() == 0){
			super.ignore(sb);
			return;
		}
		//不安全
		if(!super.isSafeAttribute(url)){
			url = "#";
		}
		
		sb.append("<a href=\"").append(url).append('"');
		if(blank){
			sb.append(" target=\"_blank\"");
		}
		sb.append(">");
		innerHTML(sb);
		sb.append("</a>");
	}

	public void setBlank(boolean blank) {
		this.blank = blank;
	}

	public boolean isBlank() {
		return blank;
	}

	@Override
	public UbbTag newInstance() {
		// TODO Auto-generated method stub
		UrlTag tag = new UrlTag();
		tag.blank = this.blank;
		return tag;
	}
	

}
